package com.walker.vo;


import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;


public class DateProcessor {

    // 日期格式化器，用于生成 yyyyMMdd 格式的日期字符串
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");

    public static List<String> generateDateList(Map<Integer, String> inputMap) {
        List<String> result = new ArrayList<>();
        LocalDate currentDate = LocalDate.now(); // 获取当前日期

        // 用于存储每个 Key 对应的日期列表
        Map<Integer, List<String>> dateMap = new HashMap<>();

        // 遍历输入 Map，解析每个 Value 并生成对应的日期列表
        for (Map.Entry<Integer, String> entry : inputMap.entrySet()) {
            String value = entry.getValue();
            List<String> dates = new ArrayList<>();

            if (value.equals("${readDay}")) {
                // 当前日期
                dates.add(currentDate.format(DATE_FORMATTER));
            } else if (value.startsWith("${readDay+") || value.startsWith("${readDay-")) {
                // 解析 ${readDay+n} 或 ${readDay-n}
                int offset = Integer.parseInt(value.replace("${readDay", "")
                        .replace("}", "")
                        .replace("+", "")
                        .replace("-", ""));
                if (value.contains("+")) {
                    // ${readDay+n}
                    dates.add(currentDate.format(DATE_FORMATTER));
                    for (int i = 1; i <= offset; i++) {
                        dates.add(currentDate.plusDays(i).format(DATE_FORMATTER));
                    }
                } else {
                    // ${readDay-n}
                    dates.add(currentDate.format(DATE_FORMATTER));
                    for (int i = 1; i <= offset; i++) {
                        dates.add(currentDate.minusDays(i).format(DATE_FORMATTER));
                    }
                }
            }

            // 将生成的日期列表存入 dateMap
            dateMap.put(entry.getKey(), dates);
        }

        // 根据 dateMap 生成最终的组合结果
        if (dateMap.size() == 1) {
            // 如果只有一个 Key，直接返回对应的日期列表
            result.addAll(dateMap.get(0));
        } else {
            // 如果有多个 Key，生成所有可能的组合
            result = generateCombinations(dateMap);
        }

        return result;
    }

    // 生成多个 Key 对应的日期组合
    private static List<String> generateCombinations(Map<Integer, List<String>> dateMap) {
        List<String> result = new ArrayList<>();
        List<List<String>> dateLists = new ArrayList<>(dateMap.values());

        // 使用递归生成所有可能的组合
        generateCombinationsHelper(dateLists, 0, new StringBuilder(), result);

        return result;
    }

    // 递归生成组合
    private static void generateCombinationsHelper(List<List<String>> dateLists, int depth, StringBuilder current, List<String> result) {
        if (depth == dateLists.size()) {
            // 到达最后一层，将当前组合加入结果
            result.add(current.toString());
            return;
        }

        for (String date : dateLists.get(depth)) {
            StringBuilder newCurrent = new StringBuilder(current);
            if (newCurrent.length() > 0) {
                newCurrent.append("\\");
            }
            newCurrent.append(date);
            generateCombinationsHelper(dateLists, depth + 1, newCurrent, result);
        }
    }

    public static void main(String[] args) {
//        // 测试用例
//        Map<Integer, String> inputMap1 = new HashMap<>();
//        inputMap1.put(0, "${readDay}");
//        System.out.println(generateDateList(inputMap1)); // [20231010]
//
//        Map<Integer, String> inputMap2 = new HashMap<>();
//        inputMap2.put(0, "${readDay+1}");
//        System.out.println(generateDateList(inputMap2)); // [20231010, 20231011]
//
//        Map<Integer, String> inputMap3 = new HashMap<>();
//        inputMap3.put(0, "${readDay-2}");
//        System.out.println(generateDateList(inputMap3)); // [20231010, 20231009, 20231008]
//
//        Map<Integer, String> inputMap4 = new HashMap<>();
//        inputMap4.put(0, "${readDay}");
//        inputMap4.put(1, "${readDay-1}");
//        System.out.println(generateDateList(inputMap4)); // [20231010\20231010, 20231010\20231009]

        Map<Integer, String> inputMap5 = new HashMap<>();
        inputMap5.put(0, "${readDay}");
        inputMap5.put(1, "${readDay-1}");
        System.out.println(generateDateList(inputMap5)); // [20231010\20231010, 20231010\20231011, 20231011\20231010, 20231011\20231011]
    }

}
